package com.maven.demo;

import org.apache.commons.io.FileUtils;
import org.apache.maven.model.Build;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.io.IOException;

/**
 * 这个 mojo 的作用是
 *
 * @author L
 */
@Mojo(name = "moveJar", defaultPhase = LifecyclePhase.PACKAGE)
public class MoveJarMojo extends AbstractMojo {

    @Parameter(defaultValue = "${default}")
    private MavenProject mavenProject;

    /**
     * 移动的目标 path，默认的值为我自己喜欢的地方
     */
    @Parameter(defaultValue = "/Users/a1-6/Desktop/res/garbageHead")
    private String path;

    @Override
    public void execute() {
        final Log log = getLog();
        log.debug("Move Jar Mojo is running");
        Build build = mavenProject.getBuild();
        // 获取当前 jar 的绝对路径
        final String absolutePath = build.getDirectory();
        String fileName = absolutePath + File.separator + build.getFinalName() + mavenProject.getPackaging();
        // 拷贝文件到制定地方
        try {
            FileUtils.copyFileToDirectory(new File(fileName), new File(path));
        } catch (IOException e) {
            throw new RuntimeException("Failed to move jar", e);
        }
    }
}
